package com.zk.babala.module;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.appcompat.widget.AppCompatButton;

/**
 * 焦点按钮, 可自动放大
 *
 * @Author: zhangke
 * @Date: 2025/8/18 16:55
 */
public class FocusableButton extends AppCompatButton {
    private float originalTextSize;
    private float focusedTextSize;

    public FocusableButton(Context context) {
        super(context);
        init();
    }

    public FocusableButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FocusableButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        originalTextSize = getTextSize();
        focusedTextSize = originalTextSize * 1.2f; // 放大1.2倍

    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            setTextSize(TypedValue.COMPLEX_UNIT_PX, focusedTextSize);
        } else {
            setTextSize(TypedValue.COMPLEX_UNIT_PX, originalTextSize);
        }
    }
}

